<?php declare(strict_types=1);


namespace App\Utils;


use App\Constants\CoContext;
use Hyperf\Utils\Context;
use Hyperf\Utils\Coroutine;

trait ShareContext
{
    /**
     * 子协程共享单次请求的协程上下文唯一 requestID
     * @return mixed
     */
    public static function requestID()
    {
        if ('-1' == Coroutine::parentId()) {
            return false;
        } else {
            $pid = Coroutine::parentId();
        }
        $requestID = Context::get(sprintf(CoContext::REQUEST_ID, $pid), null, $pid);
        return Common::setRequestID(Coroutine::id(), $requestID);
    }
}